<?php echo $header; ?>
<style>
#filter_module select {
	width:200px;
}
</style>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/report.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons">
      	<a onclick="$('#filters').slideToggle('slow');" class="button"><span>Filters</span></a>
      	<a onclick="location = '<?php echo $participents; ?>'" class="button"><span><?php echo $button_participents; ?></span></a>
      	<a onclick="location = '<?php echo $back; ?>'" class="button"><span><?php echo $button_back; ?></span></a>
      </div>
    </div>
    <div class="content">
      <?php if($filter) { ?>
      <div id="filters">
      <form action="<?php echo $filter_action; ?>" method="post" enctype="multipart/form-data" id="form">
      <table id="filter_module" class="list">
          <thead>
            <tr>
            	<td colspan="3"></td>
            	<td class="right"><a class="button" onclick="addModule();"><span>Add Critaria</span></a></td>
            </tr>
            <tr>
              <td class="left">Question</td>
              <td class="left">Critaria</td>              
			  <td class="right">Answer</td>
			  <td class="right">Action</td>
            </tr>            
          </thead>
          <?php $module_row = 1; foreach($filter_module as $key => $each_filter) { $module_row = $key + 1; ?>
          <tbody id="module-row<?=$key?>">
			<tr>
			  <td class="left"><select name="filter_module[<?=$key?>][question]" class="question">
				Loading.. Questions</select><div style="float:right" id="q_loader<?=$key?>"><img src="view/image/loading.gif" /></div></td>
			  <td class="left"><select name="filter_module[<?=$key?>][critaria]" class="critaria">
				Loading.. Critarias</select><div style="float:right" id="c_loader<?=$key?>"><img src="view/image/loading.gif" /></div></td>				
			  <td class="left"><select name="filter_module[<?=$key?>][answer]" class="answer">
<option value="0">Loading.. Answers</option>
				</select><div style="float:right" id="a_loader<?=$key?>"><img src="view/image/loading.gif" /></div></td>
			  <td class="right"><a class="button" onclick="$('#module-row<?=$key?>').remove();"><span>Remove</span></a></td>
			</tr>
			</tbody>
          <? } ?>
          <tfoot>
            <tr>
              <td colspan="4" class="right">
              	<div class="buttons">
              		<a onclick="javascript:$('#form').submit();" class="button"><span>Filter</span></a>
              		<a onclick="location = '<?php echo $reset_link; ?>'" class="button"><span>Reset</span></a>              		
              	</div>
              </td>
            </tr>          
         </tfoot>
        </table>
      </form>
      </div>
      <?php } ?>
        <table class="list">
          <thead>
            <tr>
              <td class="right"><?php if ($sort == 'i.sort_order') { ?>
                <a href="<?php echo $sort_sort_order; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_sort_order; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_sort_order; ?>"><?php echo $column_sort_order; ?></a>
                <?php } ?></td>
              <td class="left"><?php if ($sort == 'id.title') { ?>
                <a href="<?php echo $sort_title; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_title; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_title; ?>"><?php echo $column_title; ?></a>
                <?php } ?></td>
			  <td class="right"><?php echo $column_answer_stats; ?></td>
            </tr>
          </thead>
          <tbody>
            <?php if ($questions) { ?>
            <?php foreach ($questions as $question) { ?>
            <tr>
              <td class="right"><?php echo $question['sort_order']; ?></td>
              <td class="left"><?php echo $question['title']; ?></td>      
              <td class="right"><?php foreach ($question['answer'] as $answer) { ?>
                <?php echo $answer['text']; ?> : <a style="color:gray;text-decoration:none;" href="<?php echo $answer['link']; ?>"><?php echo $answer['value']; ?></a><br />
                <?php } ?></td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="4"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
var module_row = <?=$module_row?>;

$(document).ready(function() {
	<?php $module_row = 1; foreach($filter_module as $key => $each_filter) { $module_row = $key + 1; ?>
	$('#module-row<?=$key?>').find('.critaria').load('index.php?route=survey/report/options&name=critaria&select_id=<?=$each_filter['critaria'];?>&token=<?php echo $token; ?>', function(){ $('#c_loader' + <?=$key?>).hide(); });
	$('#module-row<?=$key?>').find('.question').load('index.php?route=survey/report/options&name=question&survey_id=<?php echo $survey_id; ?>&select_id=<?=$each_filter['question'];?>&token=<?php echo $token; ?>', function(){ $('#q_loader' + <?=$key?>).hide(); });
	$('#module-row<?=$key?>').find('.answer').load('index.php?route=survey/report/options&name=answer&question_id=<?=$each_filter['question'];?>&select_id=<?=urlencode($each_filter['answer']);?>&token=<?php echo $token; ?>', function(){ $('#a_loader' + <?=$key?>).hide(); });
	<?php } ?>
	
	<?php if($hide_filters) { ?>
	$('#filters').slideUp('slow');
	<?php } ?>
});


function addModule() {	
	
	html  = '<tbody id="module-row' + module_row + '">';
	html += '  <tr>';
	html += '    <td class="left"><select class="question" name="filter_module[' + module_row + '][question]">';
	html += '    <option value="0">Loading.. Questions</option>';	
	html += '    </select><div style="float:right" id="q_loader' + module_row + '"><img src="view/image/loading.gif" /></div></td>';
	html += '    <td class="left"><select class="critaria" name="filter_module[' + module_row + '][critaria]">';
	html += '    <option value="0">Loading.. Critarias</option>';
	html += '    </select><div style="float:right" id="c_loader' + module_row + '"><img src="view/image/loading.gif" /></div></td>';	
	html += '    <td class="left"><select class="answer" name="filter_module[' + module_row + '][answer]">';
	html += '    <option value="0">Select Question</option>';
    html += '    </select><div style="float:right;display:none;" id="a_loader' + module_row + '"><img src="view/image/loading.gif" /></div></td>';
	html += '    <td class="right"><a onclick="$(\'#module-row' + module_row + '\').remove();" class="button"><span>Remove</span></a></td>';
	html += '  </tr>';
	html += '</tbody>';
	
	$('#filter_module tfoot').before(html);
	
	
	$('#module-row' + module_row).find('.critaria').load('index.php?route=survey/report/options&name=critaria&select_id=<?php echo $default_critaria; ?>&token=<?php echo $token; ?>', function(){ $('#c_loader' + eval(module_row -1)).hide(); });
	$('#module-row' + module_row).find('.question').load('index.php?route=survey/report/options&name=question&survey_id=<?php echo $survey_id; ?>&select_id=0&token=<?php echo $token; ?>', function(){ $('#q_loader' + eval(module_row -1)).hide(); });
	
	$('#module-row' + module_row).find('.question').change(function() {
	  $('#a_loader' + eval(module_row - 1 )).show();
	  $(this).parent().parent().find('.answer').load('index.php?route=survey/report/options&name=answer&question_id='+this.value+'&select_id=0&token=<?php echo $token; ?>', function(){ $('#a_loader' + eval(module_row - 1 )).hide(); }); //
	});
	
	
	
	module_row++;
}
//--></script>
<?php echo $footer; ?>